package edu.arizona.sista.learn.model.displaying;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import edu.arizona.sista.learn.model.Interval;
import edu.arizona.sista.learn.model.holding.Object;
import edu.arizona.sista.learn.model.holding.Person;
import edu.arizona.sista.learn.model.holding.Scene;

/**
 * Each instance of this is an event in which a Person is watching some Object.
 */
public class WatchingSomething extends Event {
	public Person _doer;
	public Object _object;

	public WatchingSomething(int id, String label, Interval time) {
		super(id, label, time);
	}
	
	public WatchingSomething(Interval time) {
		super(time);
	}
	
	public WatchingSomething(Person doer, Object object, Interval time){
		this(time);
		_doer = doer;
		_object = object;
	}
	
	@Override
	public String toString() {
		String t = "t_"  + _time.start() + "-" + _time.end();
		String s1 = "isa(" + t + ", TimeInterval)\n";
		String s2 = "isa(" + getVariableName() + ", WatchingSomething)\n";
		String s3 = "performedBy(" + getVariableName() + ", " + _doer.getVariableName() + ")\n";  
		String s4 = "objectActedOn(" + getVariableName() + ", " + _object.getVariableName() + ")\n";
		String s5 = "temporalExtent(" + getVariableName() + ", " + t + ")\n";  
		return s1 + s2 + s3 + s4 + s5;
	}
	
	/**
	 * Load information about people looking at objects from file.
	 * @param dataFile look data file.
	 */
	public static List<WatchingSomething> loadLook(File dataFile, Scene scene) {
		List<WatchingSomething> output = new ArrayList<WatchingSomething>();
		try {
			Scanner lookScanner = new Scanner(dataFile);

			//skip first line
			lookScanner.nextLine();
			
			String lastPartID = null;
			int lastFrame = -1;
			int start = -1;
			int lastTargetId = -1;
			
			while (lookScanner.hasNextLine()) {
			    String line = lookScanner.nextLine();

				//0 partID
				//1 frame
				//2 targetId
				//3 targetLabel
				
			    // Break comma separated line using ","
				String[] tokens = line.split(",");
			    
				String partID = tokens[0];
				int frame = Integer.parseInt(tokens[1]);
				int targetId = Integer.parseInt(tokens[2]);
				
				//new event if:
				//- frame number is not incremented by 1
				//- or partID changed
				//- or targetId changed
				if (frame != lastFrame + 1 || !partID.equals(lastPartID) || targetId != lastTargetId){
					if (lastPartID != null){
						//new watching event
						Person p = scene.getPerson(lastPartID);
						Object o = (Object) scene.getSpatialThing(lastTargetId);
						Interval t = new Interval(start, lastFrame);
						WatchingSomething ws = new WatchingSomething(p, o, t);
						output.add(ws);
					}
					
					//new watching event starts
					start = frame;
				}
				lastPartID = partID;
				lastTargetId = targetId;
				lastFrame = frame;
			}
			
			//last watching event
			if (lastPartID != null){
				Person p = scene.getPerson(lastPartID);
				Object o = (Object) scene.getSpatialThing(lastTargetId);
				Interval t = new Interval(start, lastFrame);
				WatchingSomething ws = new WatchingSomething(p, o, t);
				output.add(ws);
			}
			lookScanner.close();
		} catch (Exception e) {
			System.out.println("Exception while reading csv file: " + e);
			e.printStackTrace();
		}
		for(WatchingSomething e : output) scene.addEvent(e);
		return output;
	}


}
